package cn.lin.factory;

import java.math.BigDecimal;


/**
 * @author liufl E-mail:Huberier@allpyra.com
 * @version 创建时间：2017年8月16日 上午11:17:44
 * 工厂--解决对象创建问题
 */
public class OperatorSimpleFactory {

	OperatorVo operator = null;
	
	public OperatorVo getOperator(String optType) throws Exception{
		switch (optType) {
			case "+":
				operator = new OperatorAdd();
				break;
			case "-":
				operator = new OperatorSubtract();
				break;
			case "*":
				operator = new OperatorMutiply();
				break;
			case "/":
				operator = new OperatorDevided();
				break;
	
			default:
				break;
		}
		return operator;
	}
	public OperatorVo getOperatorByReflect(String className) throws Exception{
		OperatorVo opt = (OperatorVo) Class.forName(className).newInstance();//弱类型。低效率。只能调用无参构造,Class.forName(“”)返回的是类。Class.forName(“”).newInstance()返回的是object
		return opt;
	}
	
	public static void main(String[] args) throws Exception {
		OperatorSimpleFactory simpleFactory = new OperatorSimpleFactory();
		OperatorVo operator = simpleFactory.getOperator("*");
		operator.setPrice(new BigDecimal(2.266));
		operator.setSum(3);
		System.out.println("totalPrice="+operator.getResult());
		
		OperatorSimpleFactory reflectFactory = new OperatorSimpleFactory();
		OperatorVo reflectOperator = reflectFactory.getOperatorByReflect("cn.lin.factory.OperatorMutiply");
		reflectOperator.setPrice(new BigDecimal(2.266));
		reflectOperator.setSum(3);
		System.out.println("totalPrice="+reflectOperator.getResult());
	}
}

